<?php
class Products extends MY_Controller{

	function __construct()
	{
		parent::__construct();
		if(!$this->admin) redirect('admin990/login');
	}

	function index()
	{
		if($this->uri->segment(3) ==''){
			redirect('admin990/catalogue');
		}else{
				
			if($_POST){
				if($_POST['form_id']==1){
					/* Modification principale */
					$product = product::find_by_id($_POST['id']);
					$product->product_num = $_POST['product_num'] ;
					$product->product_name = $_POST['product_name'];
					$product->product_price = $_POST['product_price'];
					$product->capture = $_POST['capture'];
					if($product->save()){
						$data['msg'] = 'Les modifications ont été enregistré ';
					}else{
						$data['msg'] = 'Il y a une erreur.';
					}
				}elseif($_POST['form_id']==2){
					/* add new attribute group */
				/*	$validation_group_name = attribute_group::find_by_attribute_group_name($_POST['attribute_group_name']);
					if($validation_group_name){
						$data['msg'] = 'Il y a déjà une option <b>"'.$_POST['attribute_group_name'].'"</b>';
					}else*/if($_POST['attribute_group_name'] ==''){
						$data['msg'] = "Le nom de l'option ne doit pas être vide!!";
							
					}else{
						$new_data = array('id'=>'','product_id'=>$_POST['product_id'],'attribute_group_name'=>$_POST['attribute_group_name'],'status'=>'1');
						$attribute_group = attribute_group::create($new_data);
						if($attribute_group){
							$data['msg'] = 'Une nouvelle option a été ajouté.';
						}else{
							$data['msg'] = 'Il y a une erreur.';
						}
					}

				}elseif($_POST['form_id']==3){
					/* change status of attribute group */
					if($_POST['status'] == 'null'){
						$status = NULL;
					}else{
						$status = $_POST['status'];
					}
					$attribute_group = attribute_group::find_by_id($_POST['group_id']);
					$attribute_group->status = $status;
					if($attribute_group->save()){
						if($attribute_group->status == !null){
							$status = 'activé';
						}else{
							$status = 'désactivé';
						}
						$data['msg'] = "L'option <b>".$attribute_group->attribute_group_name."</b> a été ".$status;
					}else{
						$data['msg'] = 'Il y a une erreur.';
					}
				}elseif($_POST['form_id']==4){
					/* delete an attribute group */
					$attribute_group_id = $_POST['group_id'];
					$product_attributes = attribute_product::find('all',array('select'=>'id','conditions'=>'attribute_group_id ='.$attribute_group_id));
					if($product_attributes != NULL){
						$attribute_id = array();
						foreach ($product_attributes as $a){
							$attribute_id[] = $a->id;
						}
							
							
						$result = attribute_product::table()->delete(array('id'=>$attribute_id));
						if($result){
							$delete = attribute_group::find_by_id($attribute_group_id);
							if($delete->delete()){
								redirect($_POST['url']);
							}else{
								$data['msg'] = 'Il y a une erreur.';
							}
								
						}else{
							$data['msg'] = 'Il y a une erreur.';
						}
							
					}else{
						$delete = attribute_group::find_by_id($attribute_group_id);
						if($delete->delete()){
							redirect($_POST['url']);
						}else{
							$data['msg'] = 'Il y a une erreur.';
						}
					}
				}elseif($_POST['form_id']==5){
					/* update product attribute */
					$attribute_price = floatval($_POST['attribute_price']);
					if($attribute_price == 0 ){
						$new_attribute_price = NULL;
					}else{
						$new_attribute_price = $attribute_price;
					}
					$product_attribute = attribute_product::find_by_id($_POST['attribute_id']);
					$product_attribute->attribute_name = $_POST['attribute_name'];
					$product_attribute->attribute_price = $new_attribute_price;
					if($product_attribute->save()){
						$data['msg'] = 'Les modifications ont été enregistré ';
					}else{
						$data['msg'] = 'Il y a une erreur.';
					}

				}elseif($_POST['form_id']==6){
					/* delete product attribute */
					$delete = attribute_product::find($_POST['attribute_id']);
					if($delete->delete()){
						redirect($_POST['url']);
					}else{
						$data['msg'] = 'Il y a une erreur.';
					}
				}elseif($_POST['form_id']==7){
					/* add new product's attribute */
					$attribute_price = floatval($_POST['attribute_price']);
					if($attribute_price == 0 ){
						$new_attribute_price = NULL;
					}else{
						$new_attribute_price = $attribute_price;
					}

					$new_data = array('id'=>'','attribute_group_id'=>$_POST['group_id'],'attribute_name'=>$_POST['attribute_name'],'attribute_price'=>$new_attribute_price);
					$product_attribute = attribute_product::create($new_data);
					if($product_attribute){
						redirect($_POST['url']);
					}else{
						$data['msg'] = 'Il y a une erreur.';
					}
				}elseif($_POST['form_id']==8){
					/* change status of attribute group */
					if($_POST['status'] == 'null'){
						$status = NULL;
					}else{
						$status = $_POST['status'];
					}
					$product = product::find_by_id($_POST['product_id']);
					$product->status = $status;
					if($product->save()){
						if($product->status == !null){
							$status = 'activé';
						}else{
							$status = 'désactivé';
						}
						$data['msg'] = "Le produit <b>".$product->product_name."</b> a été ".$status;
					}else{
						$data['msg'] = 'Il y a une erreur.';
					}
				}elseif($_POST['form_id']==9){
					$product = product::find_by_id($_POST['product_id']);
					$group_id = array();
					$attribute = array();
					foreach ($product->attribute_group_admin as $key=>$group){
						$group_id[] = $group->id;
						$attribute_id = array();
						foreach ($group->attribute_product as $attribute_product){
							$attribute_id[] = $attribute_product->id;
						}
						$attribute[] = implode(',',$attribute_id);
					}
					$attribute = explode(',', implode(',',$attribute));
					if(array_filter($attribute) !=NULL && $group_id != NULL){
						$result = attribute_product::table()->delete(array('id'=>array_filter($attribute)));
						if($result){
							$result2 = attribute_group::table()->delete(array('id'=>$group_id));
							if($result2){
								$product->delete();
								$cat = cat::find_by_id($product->cat_id);
								if($cat->parent_id ==NULL){
									redirect('admin990/catalogue/'.$product->cat_id);
								}else{
									redirect('admin990/catalogue/'.$cat->parent_id.'/'.$product->cat_id);
								}
							}else{
								$data['msg'] = 'Il y a une erreur.';
							}
						}else{
							$data['msg'] = 'Il y a une erreur.';
						}
					}elseif(array_filter($attribute) ==NULL && $group_id != NULL){
						$result = attribute_group::table()->delete(array('id'=>$group_id));
						if($result){
							$product->delete();
							$cat = cat::find_by_id($product->cat_id);
							if($cat->parent_id ==NULL){
								redirect('admin990/catalogue/'.$product->cat_id);
							}else{
								redirect('admin990/catalogue/'.$cat->parent_id.'/'.$product->cat_id);
							}
						}else{
							$data['msg'] = 'Il y a une erreur.';
						}
					}elseif (array_filter($attribute) ==NULL && $group_id == NULL){
						$product->delete();
						$cat = cat::find_by_id($product->cat_id);
						if($cat->parent_id ==NULL){
							redirect('admin990/catalogue/'.$product->cat_id);
						}else{
							redirect('admin990/catalogue/'.$cat->parent_id.'/'.$product->cat_id);
						}
					}
				}
			}
				
			$data['title'] = 'Product';
			$product_id = $this->uri->segment(3);
			$data['product'] = product::find_by_id($product_id);
			$this->load->view('admin/header',$data);
			$this->load->view('admin/catalogue/product');
				

		}

	}

	
	
}